var express = require('express');
var router = express.Router();
const sql = require('../db/index');

/**
 * @swagger
 *       /customer/saveOrUpdate:
 *       post:
 *           description: 顾客新增或者修改接口
 *           tags: [顾客]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: id
 *                 description: 顾客id
 *                 required: true
 *                 type: number
 *               - name: username,
 *                 description: 登录用户
 *                 required: true
 *                 type: string
 *               - name: password,
 *                 description: 密码
 *                 required: true
 *                 type: number
 *               - name: telephone,
 *                 description: 用户手机
 *                 required: true
 *                 type: number
 *               - name: realname,
 *                 description: 用户姓名
 *                 required: true
 *                 type: string
 *               - name: gender,
 *                 description: 用户性别
 *                 required: true
 *                 type: string
 *               - name: birth,
 *                 description: 用户生日
 *                 required: true
 *                 type: number
 *               - name: email,
 *                 description: 邮箱
 *                 required: true
 *                 type: number
 *           responses:
 *               200:
 *                   description: 操作成功
 *               500:
 *                   description: 服务器错误
 */

// 新增或者修改接口 req.body
router.post('/saveOrUpdate',async(req,res)=>{
    // 获取前端传递的参数
    const {id,username,password,telephone,realname,gender,birth,email} = req.body;
    // 如果id存在 修改操作 否则就是新增
    let mysql = '';
    if(id){
        mysql = `update base_user set username = '${username}',password = '${password}', telephone = '${telephone}',realname ='${realname}',
        gender = '${gender}',brith='${birth}',email=${email} where id=${id}
      `
    }else{
         mysql = `insert into base_user (id,username,password,telephone,realname,gender,birth,email)
     values(null,'${username}','${password}','${telephone}','${realname}','${gender}','${birth}','${email}')`
    }
    await sql.query(mysql);
    // 给前端做出响应
    res.send({
        status:200,
        message:"操作成功",
        data:null,
        timestamp:new Date().getTime()
      })


 /**
 * @swagger
 *       /customer/deleteByid:
 *       post:
 *           description: 顾客删除接口
 *           tags: [顾客]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: id
 *                 description: 顾客id
 *                 required: true
 *                 type: number
 *           responses:
 *               200:
 *                   description: 操作成功
 *               500:
 *                   description: 服务器错误
 */     
// 删除接口
 router.delete('/deleteByid',(req,res)=>{
    let {id}=req.query;
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql=`delete from jz_account_constomer where id=${id}`;
        connection.query(sql,(err,results)=>{
            if(err) throw err;
            res.json(new Response(200,null,'删除成功'))
            });
        connection.release()
    })
 });
// 给前端做出响应
res.send({
    status:200,
    message:"删除成功",
    data:null,
    timestamp:new Date().getTime()
  })




/**
 * @swagger
 *    /product/pageQuery:
 *    get:
 *        description: 分页多条件查询顾客列表
 *        tags: [顾客]
 *        parameters:
 *            - in: query
 *              name: page
 *              description: 页码
 *              required: true
 *              schema:
 *                type: number
 *            - in: query    
 *              name: pageSize
 *              description: 每页显示数量
 *              required: true
 *              schema:
 *                type: number
 *            - in: query    
 *              name: name
 *              description: 名称
 *              required: false
 *              schema:
 *                type: string
 *            - in: query    
 *              name: status
 *              description: 状态
 *              required: false
 *              schema:
 *                type: string
 *            - in: query    
 *              name: product_category_id
 *              description: 顾客所属栏目
 *              required: false
 *              schema:
 *                type: number
 *        responses:
 *          '200':
 *            description: 返回顾客列表
 */

      // 分页模糊查询
router.get('/pageQuery', async function (req, res) {
    let { page, pageSize, name, status, product_category_id } = req.query;
    page = +page;
    pageSize = +pageSize;
    product_category_id = +product_category_id;
    let result, result1;
    // 每页查询条数
    let offset = (page - 1) * pageSize;
    if (name && status && product_category_id) {
      let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' and status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name && status) {
      let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name && product_category_id) {
      let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (status && product_category_id) {
      let mysql = `select * from jz_product where status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product where status = '${status}'  and product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name || status || product_category_id) {
      if (name) {
        let mysql = `select * from jz_product where name like '${'%' + name + '%'}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where name like '${'%' + name + '%'}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      } else if (status) {
        let mysql = `select * from jz_product where status ='${status}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where status = '${status}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      } else {
        let mysql = `select * from jz_product where product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      }
    } else {
      let mysql = `select * from jz_product  limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from jz_product  limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    }
    result = result.filter(item => {
      return item.length > 0;
    })
    console.log(result[0], '2222');
    total = result1[0][0].total;
    res.send({
      status: 200,
      message: '查询成功',
      data: result[0],
      total,
      timestamp: new Date().getTime()
    })
  })
})

module.exports = router;